package com.mvvm.start.main.mvvm.v.activity

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.lifecycleScope
import androidx.paging.LoadState
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.mvvm.start.base.base.BaseActivity
import com.mvvm.start.base.utils.toast
import com.mvvm.start.common.constant.RouteUrl
import com.mvvm.start.main.databinding.ActivityPagingBinding
import com.mvvm.start.main.mvvm.v.adapter.RepoAdapter

import com.mvvm.start.main.mvvm.vm.PagingViewModle
import kotlinx.coroutines.launch


/**
 * @功能: Pagin3的使用方法
 * @User Lmy
 * @Creat 2021/11/1 10:32 上午
 * @Compony 永远相信美好的事情即将发生
 */
@Route(path = RouteUrl.CommonPagingActivity)
class PagingActivity : BaseActivity<PagingViewModle, ActivityPagingBinding>() {
    private val repoAdapter = RepoAdapter()


    @SuppressLint("WrongConstant")
    override fun initView() {

        viewBinding.recyclerView.layoutManager = LinearLayoutManager(this)
        viewBinding.recyclerView.adapter = repoAdapter
        mViewModel.getPagingData().observe(this@PagingActivity) {
            lifecycleScope.launch {
                repoAdapter.submitData(it)
            }
        }
        repoAdapter.addLoadStateListener {
            when (it.refresh) {
                is LoadState.NotLoading -> {
                    viewBinding.progressBar.visibility = View.INVISIBLE
                    viewBinding.recyclerView.visibility = View.VISIBLE
                }
                is LoadState.Loading -> {
                    viewBinding.progressBar.visibility = View.VISIBLE
                    viewBinding.recyclerView.visibility = View.INVISIBLE
                }
                is LoadState.Error -> {
                    val state = it.refresh as LoadState.Error
                    viewBinding.progressBar.visibility = View.INVISIBLE
                    "Load Error: ${state.error.message}".toast()
                }
            }
        }
    }

    override fun initObserve() {
        mViewModel.apply {
        }
    }

    override fun onResume() {
        super.onResume()
    }

    override fun initData() {

    }


}


